package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;

import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service
public class UserServiceimpl implements UserService {
    @Autowired
    UserDao userDao;
    @Autowired
    RoleDao roleDao;
    @Autowired
    PermissionDao permissionDao;
    @Autowired
    MenuDao menuDao;
    @Override
    public SysUser findByname(String username) {
        SysUser sysuser=userDao.findByname(username);
        if (sysuser==null){return null;}
        Set<Role> roles=roleDao.findRolesBySysuser(sysuser.getId());
        if (roles==null||roles.size()==0){
            return null;
        }
        for (Role role : roles) {
            Set<Permission>permissions=permissionDao.findPermission(role.getId());
            role.setPermissions(permissions);
        }
        sysuser.setRoles(roles);
        return sysuser;
    }

    @Override
    public List<Map<String, Object>> findMenu(String username) {
        List<Map<String,Object>>menuMapList=new ArrayList<>();
        SysUser sysUser = userDao.findByname(username);
        Set<Role> roles = roleDao.findRolesBySysuser(sysUser.getId());
        ArrayList<Menu> parentMenus=new ArrayList<>();
        List<Menu>childMenus=new ArrayList<>();
        for (Role role : roles) {
            List<Menu>menuList=menuDao.findMenu(role.getId());
            for (Menu menu : menuList) {
                if (menu.getLevel()==1){
                    parentMenus.add(menu);
                }else {
                    childMenus.add(menu);
                }
            }
        }
        for (Menu parentMenu : parentMenus) {
            Map<String,Object> parentMenuMap=new LinkedHashMap<>();
            List<Map<String,Object>>childList=new ArrayList<>();
            for (Menu childMenu : childMenus) {
                if (parentMenu.getId()==childMenu.getParentMenuId()){
                    Map<String,Object> childMenuMap=new LinkedHashMap<>();
                    childMenuMap.put("path",childMenu.getPath());
                    childMenuMap.put("title",childMenu.getName());
                    childMenuMap.put("linkUrl",childMenu.getLinkUrl());
                    childMenuMap.put("children",childMenu.getChildren());
                    childList.add(childMenuMap);
                    parentMenu.setChildren(childList);
                }
            }
            parentMenuMap.put("path",parentMenu.getPath());
            parentMenuMap.put("title",parentMenu.getName());
            parentMenuMap.put("icon",parentMenu.getIcon());
            parentMenuMap.put("children",parentMenu.getChildren());
                menuMapList.add(parentMenuMap);

        }


        return menuMapList;
    }
}
